package com.zy.list.create

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.zy.list.ListConstant.ARG_FILE_ID
import com.zy.list.ListConstant.ARG_PARENT_ID
import com.zy.list.R
import com.zy.storage.DataBaseUtils
import com.zy.storage.doc.DocInfo
import com.zy.utils.CanvasUtils
import com.zy.utils.data.Point
import com.zy.zlog.ZLog


//修改或者创建 title
class CreateOrModifyTitleFragment : BottomSheetDialogFragment() {

    private var type = 1
    private var parentId = 0L
    private var fileId = 0L
    private var docInfo: DocInfo? = null

    private lateinit var updateEditTitle: EditText
    private lateinit var updateEditDes: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            type = it.getInt(ARG_TYPE)
            parentId = it.getLong(ARG_PARENT_ID)
            fileId = it.getLong(ARG_FILE_ID)
        }

        if (fileId != 0L) {
            //从数据库中取出数据
            DataBaseUtils.getDocInfoDao().getDocInfo(fileId).observe(this) {
                docInfo = it
                updateEditText()
            }
        }
    }

    private fun updateEditText() {
        docInfo?.let {
            updateEditTitle.setText(it.title)
            updateEditDes.setText(it.des)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        val view = inflater.inflate(R.layout.dialog_fragment_update_title_layout, container, false)
        val updateMainTitle: TextView = view.findViewById(R.id.update_main_title)

        updateEditTitle = view.findViewById(R.id.update_edit_title)
        updateEditDes = view.findViewById(R.id.update_edit_des)

        if (type == 0) {
            updateMainTitle.text = "文件夹"
            updateEditTitle.hint = "请输入文件夹名称"
        } else {
            updateMainTitle.text = "文件"
            updateEditTitle.hint = "请输入文件名称"
        }

        view.findViewById<View>(R.id.create_cancel).setOnClickListener {
            //取消
            dismissAllowingStateLoss()
        }

        val ok: Button = view.findViewById(R.id.create_ok)
        ok.setOnClickListener {
            //新建文件/文件夹
            val title = (updateEditTitle.text ?: "未命名").toString()
            val des = (updateEditDes.text ?: "").toString()
            if (docInfo == null) {//新建
                val info = DocInfo(
                    // TODO: 临时使用当时时间作为文件id
                    fileId = System.currentTimeMillis(), //生成唯一文件ID(设备id+文件+时间)
                    parentId = parentId, title = title, des = des, fileType = type
                )
                DataBaseUtils.add(info)
            } else { //更新
                docInfo?.title = title
                docInfo?.des = des
                DataBaseUtils.update(docInfo!!)
            }


            dismissAllowingStateLoss()
            //展示修改文件名称的dialog
        }


        //添加文本变化监听
        updateEditTitle.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                ZLog.d(TAG, "beforeTextChanged $p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                ZLog.d(TAG, "onTextChanged $p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                ZLog.d(TAG, "afterTextChanged $p0")
                ok.isEnabled = p0?.length != 0
            }
        })
        return view
    }

    companion object {

        private const val TAG = "CreateOrModifyTitleFragment"
        private const val ARG_TYPE = "create_or_modify_type"

        /**
         * type 0 = 文件夹
         * type 1 = 文件
         */
        fun show(
            parentFragmentManager: FragmentManager,
            type: Int,
            parentId: Long = 0L,
            fileId: Long = 0L,
        ) {
            val ft: FragmentTransaction = parentFragmentManager.beginTransaction()
            val prev: Fragment? = parentFragmentManager.findFragmentByTag("create_update_title")
            if (prev != null) {
                ft.remove(prev)
            }
            ft.addToBackStack(null)
            val newFragment: DialogFragment = CreateOrModifyTitleFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_TYPE, type)
                    putLong(ARG_PARENT_ID, parentId)
                    putLong(ARG_FILE_ID, fileId)
                }
            }
            newFragment.show(ft, "create_update_title")
        }
    }
}